package com.alphacsp.theedge2012.ui.components.calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import com.alphacsp.theedge2012.R;
import com.alphacsp.theedge2012.data.entities.Event;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 23/10/10
 * Time: 17:44
 */
public class CalendarLayout extends ViewGroup {

    private TimeRulerView timeRulerView;
    private int numberOfTracks = 3;


    public CalendarLayout(Context context) {
        this(context, null);
    }

    public CalendarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CalendarLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        final TypedArray layoutAttributes = context.obtainStyledAttributes(attrs, R.styleable.CalendarLayout, defStyle, 0);
        this.numberOfTracks = layoutAttributes.getInt(R.styleable.CalendarLayout_tracks, numberOfTracks);
        layoutAttributes.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ensureChildren();
        timeRulerView.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int measuredHeight = timeRulerView.getMeasuredHeight();
        final int measuredWidth = timeRulerView.getMeasuredWidth();
        setMeasuredDimension(resolveSize(measuredWidth, widthMeasureSpec), resolveSize(measuredHeight, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int leftPosition, int topPosition, int rightPosition, int bottomPosition) {
        ensureChildren();
        final TimeRulerView rulerView = timeRulerView;
        rulerView.layout(0, 0, getWidth(), getHeight());

        final int rulerScale = timeRulerView.getRulerScale();
        final int rulerStartHour = timeRulerView.getStartHour();
        final int rulerDividerWidth = timeRulerView.getRulerDividerWidth();
        final int columnWidth = (getWidth() - rulerDividerWidth) / numberOfTracks;

        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() == GONE) continue;

            if (child instanceof CalendarEventButton) {
                final CalendarEventButton calendarEventButton = (CalendarEventButton) child;
                final Event event = calendarEventButton.getEvent();

                final int track = event.getTrack();

                final String startTime = event.getStartTime();
                String[] split = startTime.split(":");
                final String startHourString = split[0];
                final String startMinuteString = split[1];

                final String endTime = event.getEndTime();
                split = endTime.split(":");
                final String endHourString = split[0];
                final String endMinuteString = split[1];

                final int startHour = Integer.valueOf(startHourString);
                final int startMinute = Integer.valueOf(startMinuteString);
                final int endHour = Integer.valueOf(endHourString);
                final int endMinute = Integer.valueOf(endMinuteString);

                //Calculate the Ys
                int startY = (startHour - rulerStartHour) * rulerScale;
                startY = startY + ((rulerScale / 60) * startMinute);

                int endY = (endHour - rulerStartHour) * rulerScale;
                endY = endY + ((rulerScale / 60) * endMinute);
//                endY = startY+endY;

                //Calculate the Xs
                int startX;
                int endX;
                if (track == 0) {
                    startX = rulerDividerWidth;
                    endX = startX + (columnWidth * 3);
                } else {
                    startX = (columnWidth * (track - 1)) + rulerDividerWidth;
                    endX = startX + columnWidth;
                }

                child.layout(startX, startY, endX, endY);
            }
        }
    }

    private void ensureChildren() {
        timeRulerView = (TimeRulerView) findViewById(R.id.time_ruler);
        if (timeRulerView == null) {
            throw new IllegalStateException("Must include a R.id.time_ruler view.");
        }
        timeRulerView.setDrawingCacheEnabled(true);

//        mNowView = findViewById(R.id.blocks_now);
//        mNowView.setDrawingCacheEnabled(true);
//        if (mNowView == null) {
//            throw new IllegalStateException("Must include a R.id.blocks_now view.");
//        }
    }

    public void addEvent(CalendarEventButton calendarEventButton) {
        calendarEventButton.setDrawingCacheEnabled(true);
        addView(calendarEventButton);
    }

    public void removeAllViews() {
        ensureChildren();
        super.removeAllViews();
        addView(timeRulerView);

    }


}
